package com.yc.sole.financial.repository;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yc.sole.financial.dto.PieceRateWageDetailDto;
import com.yc.sole.financial.entity.PieceRateWageDetail;
import com.yc.sole.financial.mapper.PieceRateWageDetailMapper;
import com.yc.sole.financial.request.PieceRateWageDetailQueryReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.mybatis.util.PageUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 服务接口实现
 *
 * @author yizuomin
 * @date 2024-01-23
 **/
@Slf4j
@Service
@RequiredArgsConstructor
public class PieceRateWageDetailRepository extends ServiceImpl<PieceRateWageDetailMapper, PieceRateWageDetail> {

    /**
     * 新增
     *
     * @param pieceRateWageDtos
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void savePieceRateWage(Collection<PieceRateWageDetailDto> pieceRateWageDtos) {
        List<PieceRateWageDetail> pieceRateWageList = BeanUtil.copyToList(pieceRateWageDtos, PieceRateWageDetail.class);
        this.saveBatch(pieceRateWageList);
    }

    /**
     * 通过Id修改
     *
     * @param pieceRateWageDto
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void updatePieceRateWageById(PieceRateWageDetailDto pieceRateWageDto) {
        PieceRateWageDetail entity = new PieceRateWageDetail();
        BeanUtil.copyProperties(pieceRateWageDto, entity);
        this.updateById(entity);
    }

    /**
     * 通过id批量删除
     *
     * @param ids
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deletePieceRateWageByIds(Collection<Long> ids) {
        this.removeBatchByIds(ids);
    }

    /**
     * 通过时间和班组id批量删除
     *
     * @param shiftId
     * @param date
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByShiftIdProcessIdDate(Long shiftId, Long processId, String date) {
        LambdaQueryWrapper<PieceRateWageDetail> wrapper = Wrappers.<PieceRateWageDetail>lambdaQuery()
                .eq(PieceRateWageDetail::getShiftId, shiftId)
                .eq(PieceRateWageDetail::getProcessId, processId)
                .eq(PieceRateWageDetail::getDate, date);
        this.remove(wrapper);
    }

    /**
     * 通过时间和班组id批量删除
     *
     * @param groupIds
     * @return RestResult
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteByGroupIds(Collection<Long> groupIds) {
        LambdaQueryWrapper<PieceRateWageDetail> wrapper = Wrappers.<PieceRateWageDetail>lambdaQuery().eq(PieceRateWageDetail::getGroupId, groupIds);
        this.remove(wrapper);
    }

    /**
     * 根据id获取
     *
     * @param ids
     * @return RestResult
     */
    public List<PieceRateWageDetailDto> selectPieceRateWageByIds(Set<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            return Collections.emptyList();
        }
        return BeanUtil.copyToList(this.listByIds(ids), PieceRateWageDetailDto.class);
    }

    /**
     * 根据groupId获取
     *
     * @param groupIds
     * @return RestResult
     */
    public List<PieceRateWageDetailDto> selectByGroupIds(Set<Long> groupIds) {
        LambdaQueryWrapper<PieceRateWageDetail> wrapper = Wrappers.<PieceRateWageDetail>lambdaQuery().in(PieceRateWageDetail::getGroupId, groupIds);
        return BeanUtil.copyToList(this.list(wrapper), PieceRateWageDetailDto.class);
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return RestResult
     */
    public PieceRateWageDetailDto selectPieceRateWageById(Long id) {
        return BeanUtil.copyProperties(this.getById(id), PieceRateWageDetailDto.class);
    }

    /**
     * 分页查询
     *
     * @param page
     * @return RestResult
     */
    public Paging<PieceRateWageDetailDto> selectPieceRateWagePage(Paging page, PieceRateWageDetailQueryReq queryReq) {
        LambdaQueryWrapper<PieceRateWageDetail> queryWrapper = Wrappers.<PieceRateWageDetail>lambdaQuery()
                .eq(Objects.nonNull(queryReq.getShiftId()) && queryReq.getShiftId() > 0, PieceRateWageDetail::getShiftId, queryReq.getShiftId())
                .eq(Objects.nonNull(queryReq.getEmployeeId()), PieceRateWageDetail::getEmployeeId, queryReq.getEmployeeId())
                .eq(Objects.nonNull(queryReq.getGroupId()), PieceRateWageDetail::getGroupId, queryReq.getGroupId())
                .eq(Objects.nonNull(queryReq.getProcessId()) && queryReq.getProcessId() > 0, PieceRateWageDetail::getProcessId, queryReq.getProcessId())
                .orderByDesc(PieceRateWageDetail::getDate, PieceRateWageDetail::getProcessId)
                .and(CharSequenceUtil.isNotBlank(queryReq.getKeyword()),
                        w -> w.like(PieceRateWageDetail::getDate, queryReq.getKeyword()));
        if (CollUtil.isNotEmpty(queryReq.getDateRange())
                && CharSequenceUtil.isNotBlank(queryReq.getDateRange().get(0))
                && CharSequenceUtil.isNotBlank(queryReq.getDateRange().get(1))) {
            queryWrapper.between(PieceRateWageDetail::getDate, queryReq.getDateRange().get(0), queryReq.getDateRange().get(1));
        }
        return PageUtil.pageCopy(this.page(PageUtil.toMybatisPage(page), queryWrapper), PieceRateWageDetailDto.class);
    }
}
